// Copyright (C) joyinter
// All rights reserved.
// 
// Author : Jiang Li
// Date   : 2004-6-26

#ifndef __LISTVIEW_H_COMMON_UI__
#define __LISTVIEW_H_COMMON_UI__

#pragma once

#include "wnd.h"
#include "scrollbar.h"

class CJoyListView;

//##ModelId=44CF17B70002
enum JoyListViewSortType
{
	//##ModelId=44CF17B70004
    LVS_None,
	//##ModelId=44CF17B7000F
    LVS_Desc,
	//##ModelId=44CF17B70010
    LVS_Asce,
};

//##ModelId=44CF17B7001F
class CJoyListViewItem : public CJoyCtrl
{
protected:
    friend class CJoyListView;

	//##ModelId=44CF17B70022
    CJoyListView*   m_listView;
	//##ModelId=44CF17B7002F
    POSITION        m_selPos;

	//##ModelId=44CF17B70033
    virtual void CM_Mouse(MouseMessage msg, const CPoint& pt);
	//##ModelId=44CF17B7003E
    virtual void CM_Draw(Graphics& graph);
	//##ModelId=44CF17B7005D
    virtual void CM_SelItem(POSITION selPos);
public:
	//##ModelId=44CF17B7006D
    CJoyListViewItem();
	//##ModelId=44CF17B7007D
    virtual ~CJoyListViewItem();

	//##ModelId=44CF17B7008C
    bool IsSel()
    {
        return m_selPos != NULL;
    }
};

//##ModelId=44CF17B7009C
class CJoyListViewPanel : public CJoyCtrl
{
protected:
    friend class CJoyListView;

	//##ModelId=44CF17B700AC
    CJoyListView*   m_listView;

	//##ModelId=44CF17B700B0
    virtual void CM_Draw(Graphics& graph);
	//##ModelId=44CF17B700B3
    virtual void CM_Size(const CSize& s);
	//##ModelId=44CF17B700BD
    virtual void CM_Mouse(MouseMessage msg, const CPoint& pt);
public:
	//##ModelId=44CF17B700C1
    CJoyListViewPanel();
	//##ModelId=44CF17B700CB
    virtual ~CJoyListViewPanel();

public:
    //afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
    //afx_msg void OnSetFocus(CWnd* pOldWnd);
    //afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
};

//##ModelId=44CF17B700CD
class CJoyListViewHeader : public CJoyCtrl
{
protected:
    friend class CJoyListView;

	//##ModelId=44CF17B700DB
    CJoyListView*   m_listView;

	//##ModelId=44CF17B700DF
    virtual void CM_Size(const CSize& s);
public:
	//##ModelId=44CF17B700EA
    CJoyListViewHeader();
	//##ModelId=44CF17B700EB
    virtual ~CJoyListViewHeader();

};

//##ModelId=44CF17B700FA
class CJoyListViewField : public CJoyCtrl
{
protected:
	//##ModelId=44CF17B700FC
    virtual void CM_Draw(Graphics& graph);

	//##ModelId=44CF17B70109
    virtual void CM_Sort();
	//##ModelId=44CF17B70128
    virtual void CM_LoadSkin(CJoyXmlElement* pSkinElem);
public:
	//##ModelId=44CF17B70139
    CJoyListView*   m_listView;
	//##ModelId=44CF17B7013E
    CStringW    m_title;
	//##ModelId=44CF17B7014B
    int         m_min;
	//##ModelId=44CF17B7014C
    int         m_max;
	//##ModelId=44CF17B7014D
    float       m_percent;
	//##ModelId=44CF17B7014E
    CRect       m_itemArea;
	//##ModelId=44CF17B70157
    bool        m_sorted;
	//##ModelId=44CF17B70159
    JoyListViewSortType m_sortType;

	//##ModelId=44CF17B7015D
    virtual void CM_Mouse(MouseMessage msg, const CPoint& pt);
	//##ModelId=44CF17B70177
    virtual int CM_CompareItem(CJoyListViewItem*, CJoyListViewItem*);
public:
	//##ModelId=44CF17B70186
    CJoyListViewField();

	//##ModelId=44CF17B701A5
    void MakeAsLastScale();
	//##ModelId=44CF17B701B5
    virtual void ResetItemArea();
};

class CJoyListViewHBarWrapper;

//##ModelId=44CF17B70213
class CJoyListView :
    public CJoyWnd
{
	DECLARE_DYNAMIC(CJoyListView)
public:
	//##ModelId=44CF17B8003E
    typedef void (CJoyWnd::*PSelItem)(CJoyListViewItem* item);
protected:

    friend class CJoyListViewItem;
    friend class CJoyListViewPanel;
    friend class CJoyListViewField;
    friend class CJoyListViewHeader;
    friend class CJoyListViewHBarWrapper;

	//##ModelId=44CF17B70215
    bool m_multiSel;
	//##ModelId=44CF17B70216
    bool m_scaleWidth;


	//##ModelId=44CF17B70222
    int m_itemHeight;
	//##ModelId=44CF17B70223
    int m_itemWidth;
	//##ModelId=44CF17B70224
    int m_leftVisible;

	//##ModelId=44CF17B70226
    Color   m_frameColor;

	//##ModelId=44CF17B70233
    CJoyScrollBar*  m_vBar;
	//##ModelId=44CF17B70238
    CJoyScrollBar*  m_hBar;
	//##ModelId=44CF17B70243
    CJoyListViewHBarWrapper*    m_hBarWrapper;
	//##ModelId=44CF17B70248
    CJoyListViewHeader* m_header;

	//##ModelId=44CF17B70251
    CList<CJoyListViewItem*>    m_selItems;

	//##ModelId=44CF17B70256
    CJoyWnd*    m_wndSelItem;
	//##ModelId=44CF17B70261
    PSelItem    m_onSelItem;

	//##ModelId=44CF17B70265
    virtual CJoyListViewHeader* CreateHeader();
	//##ModelId=44CF17B70267
    virtual CJoyListViewField* CreateField();
	//##ModelId=44CF17B70272
    virtual CJoyListViewPanel* CreatePanel();
	//##ModelId=44CF17B70274
    virtual CJoyListViewItem* CreateItem();
    template<typename FieldType>
    void AddField_T(FieldType*& f, PCSTR name)
    {
        f = new FieldType();
        f->m_name = name;
        AddFieldTail(f);
    }

    
	//##ModelId=44CF17B70281
    virtual void CE_VScroll(CJoyScrollBar*, CJoyScrollBar::SCROLLBAR_MESSAGE, float&);
	//##ModelId=44CF17B70291
    virtual void CE_HScroll(CJoyScrollBar*, CJoyScrollBar::SCROLLBAR_MESSAGE, float&);
	//##ModelId=44CF17B7029F
    virtual void CM_LoadSkin(CJoyXmlElement* pSkinElem);
	//##ModelId=44CF17B702A2
    virtual void CM_Draw(Graphics& graph);
	//##ModelId=44CF17B702AF
    virtual void CM_SelItem(CJoyListViewItem* item);
public:
	//##ModelId=44CF17B702B2
    int m_firstVisible;
	//##ModelId=44CF17B702B3
    int m_firstVisibleTop;
	//##ModelId=44CF17B702C0
    CList<CJoyListViewField*>   m_fields;
	//##ModelId=44CF17B702C5
    CJoyListViewPanel*  m_itemPanel;
	//##ModelId=44CF17B702CE
    int m_hScrollStep;
	//##ModelId=44CF17B702D0
    CList<CJoyListViewItem*>    m_items;
public:
	//##ModelId=44CF17B702DE
    CJoyListView(void);
	//##ModelId=44CF17B702EE
    virtual ~CJoyListView(void);

	//##ModelId=44CF17B702FD
    int GetVisibleItemCount() const
    {
        return (m_itemPanel->GetArea().Height())/m_itemHeight;
    }
	//##ModelId=44CF17B702FF
    int GetTotalVisibleItemCount() const;
	//##ModelId=44CF17B70301
    int GetHeaderWidth() const;

	//##ModelId=44CF17B7030D
    void AddItemTail(CJoyListViewItem* item, bool rebuildArea = true);
	//##ModelId=44CF17B7031C
    void AddItemHead(CJoyListViewItem* item, bool rebuildArea = true);
	//##ModelId=44CF17B7031F
    void RemoveItem(CJoyListViewItem* item);
	//##ModelId=44CF17B7032C
    void RemoveAll();

	//##ModelId=44CF17B7032D
    void AddFieldTail(CJoyListViewField* field, bool rebuildArea = true);
	//##ModelId=44CF17B7033D
    void AddFieldHead(CJoyListViewField* field, bool rebuildArea = true);
	//##ModelId=44CF17B7034B
    void ShowHeader(bool show);

	//##ModelId=44CF17B7034D
    void BuildItemArea();
	//##ModelId=44CF17B7035B
    void BuildFieldArea();

	//##ModelId=44CF17B7035C
    void SelItem(CJoyListViewItem* item);
	//##ModelId=44CF17B7036B
    CJoyListViewItem* SelItem(int idx);
	//##ModelId=44CF17B7037A
    void UnselItem(CJoyListViewItem* item);
	//##ModelId=44CF17B7038A
    void UnselAllItem();
	//##ModelId=44CF17B7038B
    int GetItemIndex(CJoyListViewItem* item);

	//##ModelId=44CF17B70399
    void ScrollTo(int firstVisible);
	//##ModelId=44CF17B703A9
    void ScrollOffset(int step);
	//##ModelId=44CF17B703AB
    void SetSelItemEvent(CJoyWnd* pWnd, PSelItem pOnSel);

	//##ModelId=44CF17B703BB
    virtual void SortItem(CJoyListViewField* pField, JoyListViewSortType sortType);
	//##ModelId=44CF17B703D8
    void SortItem(CJoyListViewField* pField, JoyListViewSortType sortType, POSITION startPos, POSITION endPos, bool buildArea = true);

	//##ModelId=44CF17B8000F
    CJoyListViewPanel* GetPanel()
    {
        return m_itemPanel;
    }
	//##ModelId=44CF17B80010
    CJoyListViewField* GetField(int i);
	//##ModelId=44CF17B8001F
    CJoyListViewField* GetField(PCSTR pszName);
public:
    DECLARE_MESSAGE_MAP()
	//##ModelId=44CF17B8002E
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};


#endif  // __LISTVIEW_H_COMMON_UI__
